from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from lib.webui_local_mysql import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pymysql_connect import *
from pro_list_table_for_proer import  *


class c73250a:
    # name = '产品管理员录入显示产品数量相关信息'  # 测试用例名字
    # may用到文件读入产品管理员账号
    # 使用时注释其他两行，更改数据库名
    cases = [
        ('c73250a2', 'zhangsan', '123456'),
    ]

    def setup(self):
        STEP(1, '登陆网站')
        wd = open_browser()
        productor1_login(wd)
    # 清除方法
    def teardown(self):
        wd = get_global_webdriver()
        wd.quit()
    # 测试用例步骤
    def teststeps(self, para_index):
        # 取出参数
        creat_table_for_proer()
        # username, password = self.cases[para_index][1:3]
        # 下面写登录流程
        driver = get_global_webdriver()
        # driver.find_element(By.NAME, "username").send_keys(username)
        # driver.find_element(By.NAME, "password").send_keys(password)
        # driver.find_element(By.CSS_SELECTOR, ".el-button").click()

        STEP(2, "读取产品列表中查看详情到数据库")
        time.sleep(1)
        check_btn = driver.find_elements(By.CLASS_NAME,'product-card-button-third')
        check_insert_list = []
        times = 1
        for tmp_btn in check_btn:
            INFO(times)
            times += 1
            time.sleep(1)
            tmp_btn.click()
            time.sleep(1)
            product_name = driver.find_element(By.CSS_SELECTOR,
                                               'div.el-dialog__wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)'
                                               ).text
            dialog_list = driver.find_elements(By.CLASS_NAME,'detail-text')
            value_list = []
            for value in dialog_list:
                inner_list = value.find_elements_by_tag_name('span')
                tmp = inner_list[1].text
                value_list.append(tmp)
            product_tuple = (product_name,value_list[0],value_list[1],value_list[2],value_list[3],value_list[4],value_list[5])
            check_insert_list.append(product_tuple)
            # close
            INFO(check_insert_list)
            time.sleep(1)
            driver.find_element(By.CSS_SELECTOR,
                                "span.dialog-footer > button:nth-child(1) > span:nth-child(1)"
                                ).click()
            time.sleep(1)

        prev_button = driver.find_element_by_class_name("btn-next")
        if not prev_button.get_attribute("disabled"):
            INFO(prev_button.get_attribute("disabled"))
            while prev_button.get_attribute("disabled") == None:
                prev_button.click()
                time.sleep(2)
                current_insert_list = get_current_page_insert_list_product_list(driver)
                check_insert_list.extend(current_insert_list)
                time.sleep(1)
                prev_button = driver.find_element_by_class_name("btn-next")
                if prev_button.get_attribute("disabled") == "disabled":
                    break


        db = connect()
        # 使用cursor()方法获取操作游标
        INFO(3)
        cursor = db.cursor()


        # SQL 插入语句
        # 注意更换用户登录时更换表名字
        sql = "INSERT INTO product_list_for_proer(product_name,version,create_time,update_time,creater,product_function,introduction) VALUES (%s,%s,%s,%s,%s,%s,%s)"
        try:
            # 执行sql语句
            cursor.executemany(sql,check_insert_list)
            # 需要新建一个表，只包含上面的条目的
            # 提交到数据库执行
            INFO(4)
            db.commit()
            INFO(5)
            cursor.close()
        except:
            # 如果发生错误则回滚
            db.rollback()
            INFO(6)

        # 关闭数据库连接
        db.close()
        # driver.quit()
